#!/bin/sh
#
# ltsp-client-setup  Setup/Configure the LTSP client.
#
# chkconfig: 2345 00 65
# description: LTSP client initialization
# config: /usr/share/ltsp/ltsp_config
# pidfile: /var/run/ltsp-client-setip.pid

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

# Source configuration.
SourceIfNotEmpty /etc/default/ltsp-client-setup

LOCKFILE=/var/lock/subsys/ltsp-client-setup
RETVAL=0

[ -f /etc/ltsp_chroot ] && SourceIfNotEmpty /usr/share/ltsp/ltsp_config || exit 0

load_modules()
{
    for module in $(env | grep '^MODULE_' | cut -d= -f2); do
	modprobe -q $module
    done
    is_yes "$POWER_BUTTON" && modprobe -q button 2>/dev/null ||:
}

configure_localdev()
{
    if is_yes "$LOCALDEV"; then
	#[ -d /var/run/drives ] || mkdir /var/run/drives
	[ -d /tmp/drives ] || mkdir /tmp/drives
	/usr/sbin/lbuscd || true
	/usr/sbin/ltspfsd -a || true
	# cdrom devices are handled by the cdpingerponger
	#/usr/sbin/cdpinger cdrom # default for usb cdroms
	# and start one for every additional cdrom device
	#if [ -L /dev/cdrom?* ];then
	#    for CDDEV in $(ls /dev/cdrom?*); do
	#	/usr/sbin/cdpinger $(basename ${CDDEV})
	#    done
	#fi
	# handle already plugged devices on login
	#/usr/sbin/delayed_mounter
    fi
}

configure_console()
{
    [ -z "$CONSOLE_KEYMAP" ] || loadkeys "$CONSOLE_KEYMAP"
}

configure_network()
{
    hostname=$(hostname)
    if [ "(none)" = "$hostname" ] ; then
	hostname="$(hostname)"
    else
	echo $hostname > /etc/hostname
    fi
    ipaddr=`ifconfig eth0 | grep inet | awk '{print $2}' | awk -F: '{print $2}'`
    echo "127.0.0.1 localhost $hostname" > /etc/hosts
    echo "$ipaddr $hostname" >> /etc/hosts
    echo "$SERVER server" >> /etc/hosts
    [ -s /etc/hosts.ltsp ] && cat /etc/hosts.ltsp >> /etc/hosts
}

configure_resolver()
{
    if [ -n "$DNS_SERVER" -a -n "$SEARCH_DOMAIN" ]; then
	echo "search $SEARCH_DOMAIN" > /etc/resolv.conf
	echo "nameserver $DNS_SERVER" >> /etc/resolv.conf
    fi
    /sbin/update_chrooted conf
}

configure_syslog()
{
    echo "*.* @${SYSLOG_HOST-$SERVER}" > /etc/syslog.conf
}

configure_fstab()
{
    mount |
	grep -v '^rootfs ' |
	grep ' on / type nfs ' |
	sed -e 's/ (/ /' \
	    -e 's/)$/ 0 0/' \
	    -e 's| on / type | / |' >> /etc/fstab
    echo "tmpfs /tmp tmpfs defaults,nosuid,nodev 0 0" >> /etc/fstab
    mount /tmp
}

bind_unmounts()
{
    for dir in $temp_copy_dirs; do
	umount $dir
	rm -rf $tmpfs_dir/${dir#/}
    done
}

run_rcfiles()
{
    for rcfile in $(env | sort | awk -F= '$1 ~ /^RCFILE_/ { print $2 }'); do
	[ -x "$rcfile" ] && "$rcfile" $@
    done
}

start()
{
    if [ -f "$LOCKFILE" ]; then
	msg_already_running "ltsp-client-setup"
	RETVAL=1
	return $RETVAL
    fi
    msg_starting $"Setting up LTSP client"
    load_modules || true
    configure_console || true
    configure_network || true
    configure_resolver || true
    configure_fstab || true
    configure_syslog || true
    run_rcfiles || true
    configure_localdev || true
    [ "$root_write_method" = "bind_mounts" ] && bind_unmounts
    RETVAL=$?
    touch "$LOCKFILE"
    return $RETVAL
}

stop()
{
    rm -f "$LOCKFILE"
    RETVAL=0
    return $RETVAL
}

status()
{
    if [ -f "$LOCKFILE" ]; then
	echo "This service was last time (re-)started at $(LANG=C LANGUAGE=C /bin/ls -l "$LOCKFILE" --full-time | tr -s ' ' | cut -f 6-10 -d' ')."
	echo "No other status information available for this package."
	echo "All this doesn't mean that there have not been perfomed any other (not init-forced) LTSP client changes since the given time."
    else
	echo "This service hasn't been started since stopped last time."
	echo "This does mean nothing at all (stopping the service doesn't unload the LTSP client)."
    fi
    RETVAL=$?
    return $RETVAL
}

case "$1" in
    start|restart|reload)
	start
	;;
    condrestart|condreload)
	# Nothing to do on condrestart
	RETVAL=0
	;;
    stop|condstop)
	stop
	;;
    status)
	status
	;;
    *)
	msg_usage "${0##*/} {start|stop|restart|reload|status|condrestart|condreload|condstop}"
	RETVAL=1
	;;
esac

exit $RETVAL
